/* This is a small demo of the high-performance GUIX graphics framework. */

#include <stdio.h>
#include "tx_api.h"
#include "gx_api.h"
#include "gx_validation_utility.h"
#include "gx_system.h"
#include "gx_display.h"

TEST_PARAM test_parameter = {
    "guix_all_widgets_8bpp_rotated_cw_pixelmap_draw", /* Test name */
    13, 16, 463, 424  /* Define the coordinates of the capture area.
                         In this test, we only need to capture the pixelmap
                         drawing area.  */
};

static VOID      control_thread_entry(ULONG);
int main(int argc, char ** argv)
{
    /* Parse the command line argument. */
    gx_validation_setup(argc, argv);

    /* Start ThreadX system */
    tx_kernel_enter(); 
    return(0);
}

VOID tx_application_define(void *first_unused_memory)
{

    /* Create a dedicated thread to perform various operations
       on the pixelmap drawing example. These operations simulate 
       user input. */
    gx_validation_control_thread_create(control_thread_entry);

    /* Termiante the test if it runs for more than 100 ticks */
    /* This function is not implemented yet. */
    gx_validation_watchdog_create(100);

    /* Call the actual line example routine. */
    gx_validation_application_define(first_unused_memory);

}


/* Replace the default graphics driver with the validation driver. */
#ifdef win32_graphics_driver_setup_8bit_palette_rotated
#undef win32_graphics_driver_setup_8bit_palette_rotated
#endif
#define win32_graphics_driver_setup_8bit_palette_rotated  gx_validation_graphics_driver_setup_8bit_palette_rotated


#ifdef WIN32
#undef WIN32
#endif

#include "gx_validation_wrapper.h"

#include "demo_guix_all_widgets_8bpp_rotated_cw.c"

extern GX_STUDIO_DISPLAY_INFO all_widgets_8bpp_rotated_cw_display_table[];

char test_comment[256];

GX_RESOURCE_ID test_map_id_list[]=
{
GX_PIXELMAP_ID__COMPRESS_TRANS,/* Compressed, transparent. */
GX_PIXELMAP_ID__TRANS,/* Transparent. */
GX_PIXELMAP_ID__COMPRESS,/* Compress. */
GX_PIXELMAP_ID__RAW, /* Raw. */
GX_PIXELMAP_ID__JPEG,
GX_PIXELMAP_ID__PNG
};

INT test_map_id = 0;

VOID pixelmap_window_draw(GX_WINDOW *window)
{
GX_PIXELMAP *map = GX_NULL;
GX_PIXELMAP  test_map;
INT          xpos;
INT          ypos;
GX_RECTANGLE *size = &window->gx_widget_size;
GX_BRUSH     *brush;

    gx_window_draw(window);

    gx_context_pixelmap_get(test_map_id, &map);

    gx_context_brush_get(&brush);

    brush->gx_brush_style |= GX_BRUSH_SOLID_FILL;

    if(map)
    {
        gx_context_fill_color_set(GX_COLOR_ID_PURPLE);

        /* Draw map to top-left corner. */
        xpos = size->gx_rectangle_left-10;
        ypos = size->gx_rectangle_top-10;
        gx_canvas_pixelmap_draw(xpos, ypos, map);

        /* Draw map to bottom-left corner. */
        xpos = size->gx_rectangle_left-10;
        ypos = size->gx_rectangle_bottom - map->gx_pixelmap_height + 10;
        gx_canvas_pixelmap_draw(xpos, ypos, map);

        /* Draw map to top-right corner. */
        xpos = size->gx_rectangle_right - map->gx_pixelmap_width + 10;
        ypos = size->gx_rectangle_top- 10;
        gx_canvas_pixelmap_draw(xpos, ypos, map);

        /* Draw map to bottom-right corner. */
        xpos = size->gx_rectangle_right - map->gx_pixelmap_width + 10;
        ypos = size->gx_rectangle_bottom - map->gx_pixelmap_height + 10;
        gx_canvas_pixelmap_draw(xpos, ypos, map);

        /* Draw map to center. */
        xpos = (size->gx_rectangle_top + size->gx_rectangle_bottom - map->gx_pixelmap_width) >> 1;
        ypos = (size->gx_rectangle_top + size->gx_rectangle_bottom - map->gx_pixelmap_height) >> 1;
        gx_canvas_pixelmap_draw(xpos, ypos, map);
    }
}

VOID pixelmap_window_invalid_map_draw(GX_WINDOW *window)
{
GX_PIXELMAP invalid_map;
GX_PIXELMAP *map;
INT          xpos;
INT          ypos;
GX_BRUSH    *brush;

    gx_context_brush_get(&brush);
    brush->gx_brush_alpha = 255;

    xpos = window->gx_widget_size.gx_rectangle_left;
    ypos = window->gx_widget_size.gx_rectangle_top;

    /* Draw with unssuported format. */
    gx_context_pixelmap_get(GX_PIXELMAP_ID__RAW, &map);
    memcpy(&invalid_map, map, sizeof(GX_PIXELMAP));
    invalid_map.gx_pixelmap_format = GX_COLOR_FORMAT_1555XRGB;
    gx_canvas_pixelmap_draw(xpos, ypos, &invalid_map);

    brush->gx_brush_alpha = 128;

    /* Draw with unsupported flag. */
    invalid_map.gx_pixelmap_format = GX_COLOR_FORMAT_8BIT_PALETTE;
    invalid_map.gx_pixelmap_flags |= GX_PIXELMAP_ALPHA;
    gx_canvas_pixelmap_draw(xpos, ypos, &invalid_map);
}

/* This thread simulates user input.  Its priority is lower
   than the GUIX thread, so that GUIX finishes an operation 
   before this thread is able to issue the next command. */
static VOID control_thread_entry(ULONG input)
{
INT  frame_id = 1;
UINT index;
GX_CONST GX_THEME *theme_ptr;

    /* Save palette info. */
    theme_ptr = all_widgets_8bpp_rotated_cw_display_table[MAIN_DISPLAY].theme_table[MAIN_DISPLAY_THEME_1];
    gx_validation_write_palette(theme_ptr -> theme_palette, theme_ptr->theme_palette_size);
    
    /* Reset draw function of button screen to test 32argb pixelmap draw. */
    gx_widget_draw_set(&button_screen, pixelmap_window_draw);

    for(index = 0; index < sizeof(test_map_id_list)/sizeof(GX_RESOURCE_ID); index++)
    {
        test_map_id = test_map_id_list[index];

        sprintf(test_comment, "map id = %d", test_map_id);

        gx_validation_set_frame_id(frame_id++);
        gx_validation_set_frame_comment(test_comment);
        gx_validation_screen_refresh();
    }

    gx_widget_draw_set(&button_screen, pixelmap_window_invalid_map_draw);
    gx_validation_set_frame_id(frame_id++);
    gx_validation_set_frame_comment("Test invalid pixelmap draw.");
    gx_validation_screen_refresh();


    /* Signal the end of the test case.  Verify the output. */
    gx_validation_end();

    exit(0);
}
